package it.isa.demo.controller;

import it.isa.demo.entity.Book;
import it.isa.demo.entity.User;
import it.isa.demo.mapper.one.UserMapperOne;
import it.isa.demo.mapper.two.UserMapperTwo;
import it.isa.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Corgile
 * @date 2022-02-26
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping(value = "getAll")
	public List<User> getAll(Model model) {
		Map<String, Object> map = model.asMap();
		// 如果map的item不是一个元组：map.forEach(System.out::println);
		map.forEach((k, v) -> System.out.println(k + " : " + v));
		List<User> res = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			res.add(new User("侯亮平", "汉东省", i));
		}
		return res;
	}

	@PostMapping(value = "books")
	public void addBook(@ModelAttribute("book") Book book, @ModelAttribute("author") User author) {
		System.out.println(book);
		System.out.println(author);
	}

	@PutMapping(value = "doput")
	public String doput() {
		return "DO PUT";
	}

	@GetMapping(value = "getone")
	public String getUserById(Integer id) {
		return this.userService.getUserById(id);
	}

	@DeleteMapping(value = "deleteone")
	public void deleteUserById(Integer id) {
		this.userService.deleteUserNameById(id);
	}

	@Autowired
	private UserMapperOne mapperOne;
	@Autowired
	private UserMapperTwo mapperTwo;
	@RequestMapping("primary")
	public Object primary(){
		return mapperOne.getAllUsers();
	}
	@RequestMapping("secondary")
	public Object secondary  (){
		return mapperTwo.getAllUsers();
	}

}
